package com.u1zz.zeus.module.iot.service.product;

import cn.hutool.core.collection.CollUtil;
import com.u1zz.zeus.framework.common.pojo.PageParam;
import com.u1zz.zeus.framework.common.pojo.PageResult;
import com.u1zz.zeus.framework.common.util.object.BeanUtils;
import com.u1zz.zeus.module.iot.controller.admin.product.vo.ProductListReqVO;
import com.u1zz.zeus.module.iot.controller.admin.product.vo.ProductSaveReqVO;
import com.u1zz.zeus.module.iot.dal.dataobject.product.ProductDO;
import com.u1zz.zeus.module.iot.dal.dataobject.productcategory.ProductCategoryDO;
import com.u1zz.zeus.module.iot.dal.mysql.product.ProductMapper;
import com.u1zz.zeus.module.iot.service.productcategory.ProductCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.u1zz.zeus.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.u1zz.zeus.module.iot.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;

/**
 * 产品 Service
 *
 * @author u1zz
 */
@Service
@Validated
public class ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductCategoryService productCategoryService;

    public Long createProduct(ProductSaveReqVO createReqVO) {
        // 插入
        ProductDO product = BeanUtils.toBean(createReqVO, ProductDO.class);
        productMapper.insert(product);
        // 返回
        return product.getId();
    }

    public void updateProduct(ProductSaveReqVO updateReqVO) {
        // 校验存在
        validateProductExists(updateReqVO.getId());
        // 更新
        ProductDO updateObj = BeanUtils.toBean(updateReqVO, ProductDO.class);
        productMapper.updateById(updateObj);
    }

    public void deleteProduct(Long id) {
        // 校验存在
        validateProductExists(id);
        // 删除
        productMapper.deleteById(id);
    }

    private void validateProductExists(Long id) {
        if (productMapper.selectById(id) == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }

    public ProductDO getProduct(Long id) {
        List<ProductDO> list = productMapper.selectList(new ProductListReqVO().setId(id));
        fill(list);
        return CollUtil.getFirst(list);
    }

    public PageResult<ProductDO> getProductPage(ProductListReqVO reqVO, PageParam pageParam) {
        PageResult<ProductDO> page = productMapper.selectPage(reqVO, pageParam);
        fill(page.getList());
        return page;
    }

    public List<ProductDO> getProductList(ProductListReqVO reqVO) {
        List<ProductDO> list = productMapper.selectList(reqVO);
        fill(list);
        return list;
    }

    private void fill(List<ProductDO> productList) {
//        if (CollUtil.isEmpty(productList)) {
//            return;
//        }
//        for (ProductDO product : productList) {
//            if (product.getCategoryId() > 0) {
//                ProductCategoryDO category = productCategoryService.getProductCategory(product.getCategoryId());
//                product.setCategory(category);
//            }
//        }
    }

}